package com.example.vv.model.main;

import android.content.Context;
import android.os.Build;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.example.vv.R;
import com.example.vv.fragmentWork.mvp.MvpActivity;
import com.example.vv.model.account.base.AccountFragment;
import com.example.vv.model.project.ChatFragment;
import com.example.vv.model.home.HomeFragment;
import com.example.vv.model.main.base.adapter.FragmentAdapter;
import com.example.vv.model.main.base.contract.MainContract;
import com.example.vv.model.main.base.presenter.MainPresenter;
import com.example.vv.model.self.SelfFragment;
import com.example.vv.model.setup.SetupFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import org.greenrobot.eventbus.EventBus;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;

public class MainActivity extends MvpActivity<MainContract.View, MainPresenter> implements BottomNavigationView.OnNavigationItemSelectedListener, MainContract.View {


    @BindView(R.id.main_view_pager)
    ViewPager mainViewPager;
    @BindView(R.id.main_navigation)
    BottomNavigationView mainNavigation;
    private int[] menu_id;
    FragmentAdapter adapter;
    long exitTime = 0;
    long TIME = System.currentTimeMillis();;

    @Override
    public int initLayout() {
        return R.layout.activity_main;
    }

    @Override
    public MainPresenter initPresenter() {
        return new MainPresenter();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void initView() {
        mainNavigation.setItemIconTintList(null);
        menu_id = new int[]{R.id.menu_home,R.id.menu_tixi,R.id.menu_gz,R.id.menu_xm,R.id.menu_self};
        mainNavigation.setSelectedItemId(menu_id[0]);
        mainNavigation.setOnNavigationItemSelectedListener(this);
        mainViewPager.setNestedScrollingEnabled(false);
        mainViewPager.setOffscreenPageLimit(3);
        mainViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < menu_id.length; i++) {
                    if (position==i) {
                        mainNavigation.setSelectedItemId(menu_id[i]);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    public void initData() {
        adapter = new FragmentAdapter(getSupportFragmentManager());
        adapter.addFragment(new HomeFragment());
        adapter.addFragment(new SetupFragment());
        adapter.addFragment(new AccountFragment());
        adapter.addFragment(new ChatFragment());
        adapter.addFragment(new SelfFragment());
        mainViewPager.setAdapter(adapter);
        mainViewPager.setOffscreenPageLimit(6);
    }

    @Override
    public void doBusiness() {

    }

    @Override
    public View titleView() {
        return null;
    }

    @Override
    public Context getMContext() {
        return this;
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()){
            case R.id.menu_home:
                mainViewPager.setCurrentItem(0);
                return true;
            case R.id.menu_tixi:
                mainViewPager.setCurrentItem(1);
                return true;
            case R.id.menu_gz:
                mainViewPager.setCurrentItem(2);
                return true;
            case R.id.menu_xm:
                mainViewPager.setCurrentItem(3);
                return true;
            case R.id.menu_self:
                mainViewPager.setCurrentItem(4);
                return true;
        }
        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }

        return super.onKeyDown(keyCode, event);
    }
    public void exit(){
        if ((System.currentTimeMillis()-exitTime)>2000) {
            Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        }else {
            finish();
            System.exit(0);
        }
    }
}